﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace SaleOnlineSystem.Controls
{
    public partial class ViewDetail : System.Web.UI.UserControl
    {
        public string ProductName;
        public string MaSanPham;
        public string Summary;
        public string SalePrice;
        public string NewPrice;
        public string Discount;
        public string[] flag;
        public int count = 0;
        public string Product_ViewSize;
        public string deendcodesummary;
        public string linkbaohanh;
        public string Product_ViewWarranty;
        public static int check = 0;
        public int idproductcolor;
        public int idproductsize;
        public string hotsdt;
        public string color;


        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {

                DataTable dt = ProductDetail.dt;
                if (dt != null)
                {
                    ProductName = dt.Rows[0]["Product_Name"].ToString();
                    MaSanPham = dt.Rows[0]["Product_ID"].ToString();
                    Summary = Server.HtmlDecode(dt.Rows[0]["Product_Summary"].ToString());
                    Product_ViewSize = dt.Rows[0]["Product_ViewSize"].ToString();
                    Product_ViewWarranty = dt.Rows[0]["Product_ViewWarranty"].ToString();
                    idproductcolor = Convert.ToInt32(dt.Rows[0]["ProductColor_ID"].ToString());
                    double sale = Convert.ToDouble(dt.Rows[0]["Product_SalesPrice"].ToString());
                    double dis = Convert.ToDouble(dt.Rows[0]["Product_Discount"].ToString());
                    linkbaohanh = dt.Rows[0]["Product_LinkWarranty"].ToString();

                    //  Gia chua sua
                    double newp = Math.Ceiling(sale - sale * dis / 100);
                    SalePrice = LIB.Converter.toCurency(sale.ToString());
                    Discount = dis.ToString(); ;
                    NewPrice = LIB.Converter.toCurency(newp.ToString());

                    rpt_Size.DataSource = DAL.DCollector.DBCollector.Instance.StoredProcedureGetTableByID("tbl_ProductSize", idproductcolor, "selectorsize");
                    rpt_Size.DataBind();

                    DataTable tblColor = DAL.DCollector.DBCollector.Instance.StoredProcedureGetTableByID("tbl_ProductColor,tbl_Color", ProductDetail.id, "selectorsamecolor");
                    for (int i = 0; i < tblColor.Rows.Count; i++ )
                    {
                        color = tblColor.Rows[i]["Color_Name"].ToString();
                       
                    }
                    
                    tblColor.Columns.Add("Check", typeof(String));

                    string id = "";
                    flag = new string[tblColor.Rows.Count];

                    for (int i = 0; i < tblColor.Rows.Count; i++)
                    {
                        id = tblColor.Rows[i]["Product_ID"] + "-" + tblColor.Rows[i]["Color_ID"];
                        if (BOL.Shopping.ShoppingCart.Instance.checkID(id) == true)
                        {
                            tblColor.Rows[i]["Check"] = "";
                            flag[i] = "1";
                        }
                        else
                        {
                            tblColor.Rows[i]["Check"] = "hidden";
                            flag[i] = "0";
                        }
                    }

                    rpt_color.DataSource = tblColor;

                    rpt_color.DataBind();
                }

                dt = DAL.DCollector.DBCollector.Instance.SelectSQL("Select * from tbl_Product,tbl_ProductSize,tbl_Size,tbl_ProductColor where tbl_ProductColor.Product_ID = tbl_Product.Product_ID AND tbl_ProductSize.ProductColor_ID = tbl_ProductColor.ProductColor_ID AND tbl_ProductSize.Size_ID = tbl_Size.Size_ID AND tbl_ProductColor.ProductColor_ID = " + idproductcolor);
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    ListItem lt = new ListItem(dt.Rows[i]["Size_Name"].ToString(), dt.Rows[i]["ProductSize_ID"].ToString());
                    ddlLoadSize.Items.Add(lt);
                }
                if (dt.Rows.Count > 0)
                {
                    ddlLoadSize.SelectedIndex = 0;
                    idproductsize = Convert.ToInt32(dt.Rows[0]["ProductSize_ID"].ToString());
                    submit.Enabled = true;
                }
                else
                {
                    submit.Enabled = false;
                }

                dt = DAL.DCollector.DBCollector.Instance.StoredProcedureSelectByColumns("tbl_Header", " * ", "selectbycolumns");
                hotsdt = dt.Rows[0]["Header_Name3"].ToString();
            }
        }

        
        protected void submit_Click(object sender, EventArgs e)
        {

            string sp = Request["cart"].ToString();
            sp += "-"+ddlLoadSize.SelectedValue;
            string []temp = sp.Split('-');
            if (temp.Length == 3)
            {
               
               BOL.Shopping.ShoppingCart.Instance.AddItem(sp);
                
                BOL.Shopping.ShoppingCart.Instance.UpdateDataItems();
                Response.Redirect("/giohang.htm");
            }
            else
            {
                Response.Redirect("/" + LIB.Converter.convertLink(ProductDetail.dt.Rows[0]["Product_Name"].ToString()) + "/" + ProductDetail.dt.Rows[0]["Product_ID"].ToString() + "/chitietsanpham.htm");

            }
        }

        protected void rpt_color_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            //if (e.Item.ItemType == ListItemType.Item)
            //{
            //    Label lb1 = (Label)e.Item.FindControl("temp1");
            //    Label lb2 = (Label)e.Item.FindControl("temp2");
            //    flag = BOL.Shopping.ShoppingCart.Instance.checkID(lb1.Text + "-" + lb2.Text);
            //}
        }

        protected void rpt_color_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            if (e.CommandName == "changeImage")
            {
                // Xoa
                string productcolorid = e.CommandArgument.ToString();

                ProductDetail.dt = DAL.DCollector.DBCollector.Instance.StoredProcedureGetTableByID("tbl_Product", Convert.ToInt32(productcolorid), "selectordetailproductcolor");


                Response.Redirect("/" + LIB.Converter.convertLink(ProductDetail.dt.Rows[0]["Product_Name"].ToString()) + "/" + ProductDetail.dt.Rows[0]["Product_ID"].ToString() + "/color/" + ProductDetail.dt.Rows[0]["ProductColor_ID"].ToString() + "/chitietsanpham.htm");

            }
        }
    }
}